// page_two/dtwb/dtwb.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
    ...request,
    /**
     * 页面的初始数据
     */
    data: {
        yuming: yuming,
        title: '维保单位',
        buttonClicked: false,
        elevator: '请选择维保单位', //电梯
        elelist: [{
                id: 1,
                name: '自动扶梯',
            },
            {
                id: 2,
                name: '自动人行道',
            },
            {
                id: 3,
                name: '乘客电梯',
            },
            {
                id: 4,
                name: '载货电梯',
            }
        ], //  电梯
        // elelist: [],
        eleindex: 0, // 电梯
        zhu_riqi_guo: '请选择注册登记日期', //注册登记日期
        dibu_img: '',
        touyri_guo: '请选择投用日期', //投用日期
        shebeimingc: '', //设备名称
        shebeixinghao: '', //设备型号
        danwei_dizhi: '', //单位地址
        dianhua: '', //电话
        nianjian_guo: '请选择年检日期', //年检日期
        toubao_guo: '请选择投保日期', //投保日期
        tezhong_img: '', //特种设备登记证
        wudtezhong_img: '',
        hetong: '', //合同
        hetong_name: '', //合同名字
        hetong_url: '', //合同链接
        photo: [],
        wuphoto: '',
        dianzi_img: '', //电子版上传
        wudianzi_img: '',
        weibaotype_list: [{
            name: '1'
        }], //维保类型
        weibaotype_index: '',
        weibao_guo: '请选择维保类型', //
    },
    // 上传
    shangchuan(e) {
        console.log(e)
        var state = e.currentTarget.dataset.state
        var that = this;
        this.myChooseImage(1).then(res => {
            const tempFilePaths = res.tempFilePaths
            this.myUploadImg(tempFilePaths[0], 'file').then(res => {
                var data = JSON.parse(res.data)
                wx.showToast({
                    title: data.msg,
                    icon: 'none'
                })
                var urls = data.httpUrl + data.imgUrl
                // 安全管理人员证
                if (state == 0) {
                    that.setData({
                        dibu_img: urls,
                        wudibu_img: data.imgUrl
                    });
                }
                // 特种设备登记证
                if (state == 1) {
                    that.setData({
                        tezhong_img: urls,
                        wudtezhong_img: data.imgUrl
                    });
                }
                // 电子版上传
                if (state == 2) {
                    that.setData({
                        dianzi_img: urls,
                        wudianzi_img: data.imgUrl
                    });
                }
            })
        })
    },
    fangda() {
        console.log('点击了放大！')
        wx.previewImage({
            urls: [this.data.dibu_img]
        })
    },
    huan() {
        var that = this
        wx.showModal({
            title: '您想实现哪种操作？',
            cancelText: '放大',
            confirmText: '重新上传',
            success: function (res) {
                if (res.confirm) {
                    console.log("重新上传")
                    that.shangchuan()
                } else {
                    console.log("放大")
                    that.fangda()
                }
            }
        })
    },
    bininput(e) {
        let val = e.detail.value
        let modal = e.currentTarget.dataset.modal
        this.setData({
            [modal]: val
        })
    },
    // 设备类型
    elevator(e) {
        let index = e.detail.value
        this.setData({
            elevator: this.data.elelist[index].name,
            eleid: this.data.elelist[index].id,
        });
    },
    // 维保类型
    bind_weitype(e) {
        let index = e.detail.value
        this.setData({
            weibao_guo: this.data.weibaotype_list[index].name
        });
    },
    // 设备注册日期
    shebei_zhu_date(e) {
        this.setData({
            zhu_riqi_guo: e.detail.value
        });
    },
    // 投用日期
    touyong_date(e) {
        this.setData({
            touyri_guo: e.detail.value
        });
    },
    // 年检日期
    bind_dianjian(e) {
        this.setData({
            nianjian_guo: e.detail.value
        })
    },
    // 投保日期
    bind_toubao(e) {
        this.setData({
            toubao_guo: e.detail.value
        })
    },
    //单选
    radioChange(e) {
        let index = e.currentTarget.dataset.index;
        this.data.ans[index].a = e.currentTarget.dataset.title;
        this.data.ans[index].b = e.detail.value;
        this.data.ans[index].c = true;
        this.setData({
            ans: this.data.ans
        })
        let count = 0;
        this.data.ans.forEach(item => {
            if (item.c) {
                count++
            }
        })
        this.setData({
            count
        })
        console.log(this.data.ans);
    },
    // 差掉
    chadiao(e) {
        var index = e.currentTarget.dataset.index
        var photo = this.data.photo;
        var wuphoto = this.data.wuphoto;
        var dd = wuphoto.split(",")
        console.log(dd)
        photo.splice(index, 1)
        dd.splice(index, 1)
        var rr = dd.join(",")
        this.setData({
            photo: photo,
            wuphoto: rr
        })
    },
    // 验证
    ti() {
        if (this.data.elevator == '请选择维保单位') {
            wx.showToast({
                title: '请选择电梯',
                icon: 'none'
            })
        } else if (this.data.danname == '') {
            wx.showToast({
                title: '请输入单位名称',
                icon: 'none'
            })
        } else if (this.data.wbdanname == '') {
            wx.showToast({
                title: '请输入维保单位名称',
                icon: 'none'
            })
        } else if (this.data.wbrname == '') {
            wx.showToast({
                title: '请输入维保人姓名',
                icon: 'none'
            })
        } else if (this.data.count != this.data.question.length) {
            wx.showToast({
                title: '请回答完所有问题',
                icon: 'none'
            })
        } else if (this.data.aqyh == '') {
            wx.showToast({
                title: '请输入安全隐患记录',
                icon: 'none'
            })
        } else if (this.data.photo.length == 0) {
            wx.showToast({
                title: '请上传现场维护照片',
                icon: 'none'
            })
        } else {
            this.que()
        }
    },
    // 返回
    backs() {
        wx.navigateBack({
            delta: 0
        })
    },
    // 上传合同
    chuan_he() {
        wx.chooseMessageFile({
            count: 6,
            type: 'file',
            extension: ['xlsx', 'xls', 'doc', 'docx', 'zip', 'rar', 'war', 'png', 'jpg', 'pdf'],
            success: res => {
                console.log(res)
                this.setData({
                    hetong_name: res.tempFiles[0].name,
                    hetong_url: res.tempFiles[0].path,
                    hetong: res.tempFiles[0],
                })
            },
        })
    },
    xia_he() {
        var that = this;
        wx.downloadFile({
            url: that.data.hetong_url,
            success(res) {
                const tempFilePath = res.tempFilePath
                wx.saveFile({
                    tempFilePath,
                    success(res) {
                        console.log(res)
                        wx.showToast({
                            title: '下载成功',
                            icon: "none"
                        })
                        const savedFilePath = res.savedFilePath
                        var a = savedFilePath.split(".")
                        var fileType = a[1];
                        console.log(fileType)
                        wx.openDocument({
                            filePath: savedFilePath,
                            fileType: fileType,
                            success(res) {
                                console.log("打开文档成功")
                            }
                        })
                    },
                    fail(err) {
                        // console.log('保存失败')
                    }
                })
            },
            fail(err) {
                // console.log('下载失败')
            }
        })
        // wx.downloadFile({
        //   url: that.data.hetong_url,
        //   success: function (res) {
        //     console.log(res)
        //     var path = res.tempFilePath;
        //     var a = path.split(".")
        //     console.log(a)
        //     var fileType = a[1];
        //     console.log(path)
        //     console.log(fileType)
        //     wx.openDocument({
        //       filePath: path,
        //       fileType: fileType,
        //       success: function (res) {
        //         console.log(res)
        //         wx.showToast({
        //           title: '成功',
        //         })
        //       },
        //       fail(err) {
        //         console.log(err)
        //         wx.showToast({
        //           title: err.errMsg,
        //         })
        //       }
        //     })
        //   }
        // })
    },
    // 拍照上传
    // 上传
    chuan(e) {
        var that = this
        var pics = [];
        var wuphoto = this.data.wuphoto;
        this.myChooseImage(9).then(res => {
            const tempFilePaths = res.tempFilePaths
            for (var i = 0; i < tempFilePaths.length; i++) {
                pics.push(tempFilePaths[i])
            }
            if ((pics.length) + (that.data.photo.length) > 9) {
                wx.showToast({
                    title: '最多可上传' + 9 + "张",
                    icon: 'none'
                })
            } else {
                for (var i = 0; i < tempFilePaths.length; i++) {
                    (function (i) { // 注意这里是形参
                        setTimeout(function () {
                            that.wuduoupimg(tempFilePaths[i]).then(res => {
                                var data = JSON.parse(res.data)
                                var urls = data.httpUrl + data.imgUrl
                                wuphoto = wuphoto.concat(data.imgUrl + ",")
                                that.setData({
                                    photo: that.data.photo.concat(urls),
                                    wuphoto: wuphoto
                                });
                            })
                        }, 800 * i); // 还是每秒执行一次，不是累加的
                    })(i) // 注意这里是实参，这里把要用的参数传进去
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            heights: wx.getMenuButtonBoundingClientRect().top + 5,
            shopType: options.shopType,
            title: options.title
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
})